<template>
	<view class="bind">
		<u-navbar  :autoBack="true"  title="绑定健康卡"></u-navbar>
		<view class="">
			<health-card-users type="auth" hospitalId="100001" @select="selectCallback()" @addCard="addCard()" @switchType="switchTypeCallback()" />
		</view>
		<u-popup customStyle="width:700rpx;" :show="show" :round="10" mode="center" width="80%" :closeable="true" @close="closee" height="" border-radius="10" >
			<view class="pop">
		<!-- 	<view class="poptop">
				<u-navbar :custom-back="cancel" :is-back="true" :title-bold="true" title="健康卡授权"></u-navbar>
			</view> -->
			<view class="cardimg">
				<image src="../../../static/icons/cardicon.png" mode=""></image>
			</view>
			<view class="hosname">
			河南预约挂号平台
			</view>
			<view class="" v-if="showpp">
				<health-card-login
				           encrypt="true"
				           authLogin="false"
				           @authFail="authFail"
				          @authSucess="authSuccess"
				          @authCancel="authCancel"
				           wechatcode="true"
				       >
					<view class="foott">
						<view @click="hideComponentLogin()" class="agree">一键登录授权</view>
					</view>
					<view class="shuomingg">
						登录代表同意<span @click="goxieyi()">《用户服务协议与隐私政策》</span>并授权使用您的账号信息以便您统一管理
					</view>
						        </health-card-login>
			</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
            show:false,
			wechatCode:"",
			showpp:false,
			hosname: "",
			type:""
			}
		},
		onLoad(options) {
			this.hosname = uni.getStorageSync("hosname")
			this.type=options.type
		},
		methods: {
			goxieyi(){
				uni.navigateTo({
					url:"../../xieyi/xieyi"
				})
			},
			cancel(){
				this.show=false
				this.showpp=false
			},
			login() {
							  const plugin = requirePlugin('myPlugin')
			      // plugin.login(callback, options)
			      plugin.login((isok, res) => {
			          console.log('plugin.login: ', isok, res)
			          if (res.result.type !== 3) {
			              // 用户在微信授权过，可直接获取登录信息，处理后续业务
			              this.todo(res)
			          } else {
			              // 用户未授权，需要用户同意授权
			              // 显示 healthCardLogin 登录组件，引导用户同意授权
			             this.show=true
						 this.showpp=true
			          }
			      }, {
			          wechatcode: true
			      })
			  },
						
			  // 隐藏授权登录组件
			  hideComponentLogin() {
			      this.show=false
				  this.showpp=false
			      uni.showLoading()
			  },
						
			  // 用户同意授权，授权成功回调
			  authSuccess(data) {
			      uni.hideLoading()
			      let res = data.detail
			      // 同 plugin.login，用户同意授权，获取登录信息，处理后续业务
			      this.todo(res)
			  },
						
			  // 用户取消授权，授权失败回调
			  authCancel() {
			      uni.hideLoading()
								
			  },
						

			  todo(res){
			      this.wechatCode = res.result.wechatCode
				  if(this.wechatCode){
				  	uni.navigateTo({
				  		url: `../addcard/addcard?wechatCode=${this.wechatCode}&type=1`
				  	})
				  }else{
				  	this.login()
				  }
			
			  },
			// 选卡回调
			selectCallback(result) {
				console.log('selectCallback: ', result)
				let data = result.detail || {}
				let {
					healthCode = ''
				} = data
				if(healthCode){
					
					// if(this.type==2){
						uni.setStorageSync("healthCode",healthCode)
					// 	uni.navigateBack({
					// 	    delta: 2
					// 	});
					// }else{
					// 	uni.navigateTo({
					// 		url:`../people/people?healthCode=${healthCode}`
					// 	})
					// }
					let pages = getCurrentPages(); // 当前页面 
					let beforePage = pages[pages.length - 2]; // 前一个页面
					uni.navigateBack({
						success: function() {
							beforePage.onLoad(healthCode); // 执行前一个页面的onLoad方法
						},
					});	
					// uni.navigateTo({
					// 	url:`/pages/mine/addcard/addcard?healthCode=${healthCode}`
					// })
				}
			  
			},

			// 点击添加用卡人
			addCard(event) {
				if(this.wechatCode){
					uni.navigateTo({
						url: `../addcard/addcard?wechatCode=${this.wechatCode}`  
					})
				}else{
					this.showpp=true
				}
							
			},
			closee(){
				this.show=false
			}
		}
	}
</script>
<style>
	.u-modal__content{
		text-align: center;
	}
	
</style>
<style lang="scss" scoped>
.bind{
	width: 100%;
	padding-top: 180rpx;
.pop {
		width: 90%;
		margin: 0 auto;
		overflow: hidden;
		margin-top: 150rpx;
		padding-bottom: 30rpx;
	
		.poptop {
			width: 100%;
		}
	
		.cardimg {
			width: 150rpx;
			height: 150rpx;
			margin: 0 auto;
			margin-top: 10rpx;
	
			image {
				width: 100%;
				height: 100%;
			}
		}
	
		.hosname {
			width: 95%;
			margin: 0 auto;
			text-align: center;
			font-size: 50rpx;
			font-weight: bold;
			margin-top: 80rpx;
		}
	
	
	
		.title {
			width: 100%;
			text-align: center;
			margin-top: 18rpx;
			font-size: 32rpx;
			color: #333;
			font-weight: 550;
		}
	
		.info {
			width: 80%;
			margin: 0 auto;
			margin-top: 10rpx;
			font-size: 28rpx;
		}
	
		.shuomingg {
			width: 95%;
			margin: 0 auto;
			font-size: 28rpx;
			color: #999;
			line-height: 40rpx;
			margin-top: 80rpx;
	
			span {
				color: orange;
			}
	
		}
	
		.foott {
			width: 80%;
			margin: 0 auto;
			display: flex;
			// margin-top: 20rpx;
			justify-content: space-between;
	
	
	
			.agree {
				width: 80%;
				height: 90rpx;
				margin: 0 auto;
				font-size: 34rpx;
				color: #fff;
				border-radius: 90rpx;
				line-height: 90rpx;
				text-align: center;
				background-color: #3669F8;
				margin-top: 100rpx;
			}
		}
	}
	
}
</style>
